package com.djzz.app.wdy.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.net.http.SslError;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.djzz.app.wdy.R;
import com.djzz.app.wdy.base.BaseActivity;
import com.djzz.app.wdy.definations.keys.IntentKeys;

import java.util.List;

import butterknife.BindView;

/**
 * html加载
 * Created by haohe on 2017/6/5 0005.
 */

public class WebActivity extends BaseActivity {


    @BindView(R.id.com_back)
    ImageView comBack;
    @BindView(R.id.com_title)
    TextView comTitle;
    @BindView(R.id.web)
    WebView web;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void init() {

        String url = getIntent().getExtras().getString(IntentKeys.WEB_URL );
        WebSettings webSettings = web.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSavePassword(false);

        web.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("http://")
                        || url.startsWith("https://")) {
                    return false;
                } else if (url.startsWith("mailto:")) {
                    Uri uri = Uri.parse(url);
                    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
                    List<ResolveInfo> availableSoft =
                            getPackageManager().queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY);
                    if (availableSoft.size() > 0) {
                        try {
                            startActivity(emailIntent);
                        } catch (Exception e) {
                        }
                    }
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

        web.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });

        web.loadUrl(url);


        comBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
//                setAnimationOut();
            }
        });
        comTitle.setText("");
    }
}
